﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

/// <summary>
/// Summary description for Email
/// This is the class used to send the Email alert to a given/ selected registered user
/// Use two methods & smmtp server is smtp.gmail.com 
/// Can use in any application for any purpose 
/// Sends with our Authentication
/// Completely secured. Use SSL V3 & Https & a Encoded message
/// </summary>
public class Email
{
    public Email()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    /// <summary>
    /// This is the primery method try from the program
    /// Latest & easiest method of sending an Email using ASP.NET
    /// </summary>
    /// <param name="to"></param>
    /// <param name="from"></param>
    /// <param name="subject"></param>
    /// <param name="body"></param>
   public static void sendMail(string hoten,string giotinh,string diachi,string dienthoai,string email, string thongtinthem,string donhang)
    {
        string to = System.Configuration.ConfigurationManager.AppSettings["Email"];
        string from = "giarethoitrang@gmail.com";
        ///Smtp config
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
	// Edit password and username
        client.Credentials = new NetworkCredential(from, "T@m0011111985");//user va pass
        client.EnableSsl = true;
       // convert ngay
        string d = "";
        DateTime dt = DateTime.Now;
        if (dt.DayOfWeek == DayOfWeek.Monday)
        {
            d = "Thứ 2";
        }else
        if (dt.DayOfWeek == DayOfWeek.Tuesday)
        {
            d = "Thứ 3";
        }
        else
            if (dt.DayOfWeek == DayOfWeek.Wednesday)
            {
                d = "Thứ 4";
            }
            else
                if (dt.DayOfWeek == DayOfWeek.Thursday)
                {
                    d = "Thứ 5";
                }
                else
                    if (dt.DayOfWeek == DayOfWeek.Friday)
                    {
                        d = "Thứ 6";
                    }
                    else
                        if (dt.DayOfWeek == DayOfWeek.Saturday)
                        {
                            d = "Thứ 7";
                        }
                        else
                            if (dt.DayOfWeek == DayOfWeek.Sunday)
                            {
                                d = "Chủ Nhật";
                            }
        ///mail details
        MailMessage msg = new MailMessage();
        try
        {
            msg.From = new MailAddress(from);
            msg.To.Add(to);
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Subject = "Có khách mua hàng - "+d+" - "+ DateTime.Now.ToString("dd/MM/yyyy - hh:mm:ss tt");
            //msg.CC.Add();
            msg.IsBodyHtml = true;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            StringBuilder body = new StringBuilder();
            body.Append("<html><body><table width='100%'>");
            body.Append("<tr><td><span style='font-size:13px;font-weight:bold;color:#da0750;text-decoration:underline'>THÔNG TIN CHI TIẾT ĐƠN HÀNG</span></td></tr>");
            body.Append("<tr><td><span style='font-size:13px;font-weight:bold;color:#da0750'>Họ tên: " + hoten.ToUpper() + "</span></td></tr>");
            body.Append("<tr><td><span style='font-size:13px;font-weight:bold;color:#da0750'>Giới tính: " + giotinh + "</span></td></tr>");
            body.Append("<tr><td><span style='font-size:13px;font-weight:bold;color:#da0750'>Địa chỉ: " + diachi.ToUpper() + "</span></td></tr>");
            body.Append("<tr><td><span style='font-size:13px;font-weight:bold;color:#da0750'>Điện thoại: " + dienthoai + "</span></td></tr>");
            if (email != "")
            {
                body.Append("<tr><td><span style='font-size:13px;font-weight:bold;color:#da0750'>Email: " + email + "</span></td></tr>");
            }
            if (thongtinthem != "")
            {
                body.Append("<tr><td><span style='font-size:13px;font-weight:bold;color:#da0750'>Thông tin thêm: " + thongtinthem + "</span></td></tr>");
            }
            body.Append("" + donhang + "");
            body.Append("</table></body></html>");
            msg.Body = body.ToString();
            msg.Priority = MailPriority.High;
            // Enable one of the following method.
            client.Send(msg);

           }
        catch (Exception exp)
        {
            throw exp;
        }

        
    }
   public static string Convert_ngay()
   {
       string d = "";
       DateTime dt = DateTime.Now; 
           if (dt.DayOfWeek == DayOfWeek.Monday)
            {
                 d = "Thứ 2";
            }
            if (dt.DayOfWeek == DayOfWeek.Tuesday)
            {
                 d = "Thứ 3";
            }else
            if (dt.DayOfWeek == DayOfWeek.Wednesday)
            {
                d = "Thứ 4";
            }else
            if (dt.DayOfWeek == DayOfWeek.Thursday)
            {
               d = "Thứ 5";
            }else
            if (dt.DayOfWeek == DayOfWeek.Friday)
            {
                d = "Thứ 6";
            }else
            if (dt.DayOfWeek == DayOfWeek.Saturday)
            {
                d = "Thứ 7";
            }else
           {
                d = "Chủ nhật";
            }
            return d;
   }
   public static void sendMail_LienHe(string hoten, string dienthoai,string email,string body)
   {
       StringBuilder strBody = new StringBuilder();
       string to = System.Configuration.ConfigurationManager.AppSettings["Email"];
      // string cc = System.Configuration.ConfigurationManager.AppSettings["cc"];
       string from = "giarethoitrang@gmail.com";
       ///Smtp config
       SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
       // Edit password and username
       client.Credentials = new NetworkCredential(from, "T@m0011111985");//user va pass
       client.EnableSsl = true;
       ///mail details
       MailMessage msg = new MailMessage();
       try
       {
           msg.From = new MailAddress(from);
           msg.To.Add(to);
           msg.SubjectEncoding = System.Text.Encoding.UTF8;
           msg.Subject = "THÔNG TIN LIÊN HỆ";
           //msg.CC.Add();
           msg.IsBodyHtml = true;
           msg.BodyEncoding = System.Text.Encoding.UTF8;
           strBody.Append("<html><body><table width='100%'>");
           strBody.Append("<tr><td></td><td><span style='font-size: 15px;font-weight:bold;color:#da0750;'>Người gửi: " + hoten + "</span></td></tr>");
           strBody.Append("<tr><td></td><td><span style='font-size: 15px;font-weight:bold;color:#da0750;'>Điện thoại: " + dienthoai + "</span></td></tr>");
           strBody.Append("<tr><td></td><td><span style='font-size: 15px;font-weight:bold;color:#da0750;'>Email: " + email + "</span></td></tr>");
           strBody.Append("<tr></td><td><td align='center' valign='middle'>___________________________:</td></tr>");
           strBody.Append("<tr></td><td><td>" + body + "</td></tr>");
           strBody.Append("</table></body></html>");

           msg.Body = strBody.ToString();
           msg.Priority = MailPriority.High;
           // Enable one of the following method.
           client.Send(msg);

       }
       catch (Exception exp)
       {

       }

   }
}
